package cn.iocoder.yudao.module.iot.service.devicedataupload;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.iot.controller.admin.devicedataupload.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicedataupload.DeviceDataUploadDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.iot.convert.devicedataupload.DeviceDataUploadConvert;
import cn.iocoder.yudao.module.iot.dal.mysql.devicedataupload.DeviceDataUploadMapper;

import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.DEVICE_DATA_UPLOAD_NOT_EXISTS;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 物联网设备上报数据记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DeviceDataUploadServiceImpl implements DeviceDataUploadService {

    @Resource
    private DeviceDataUploadMapper deviceDataUploadMapper;

    @Override
    public Integer createDeviceDataUpload(DeviceDataUploadCreateReqVO createReqVO) {
        // 插入
        DeviceDataUploadDO deviceDataUpload = DeviceDataUploadConvert.INSTANCE.convert(createReqVO);
        deviceDataUploadMapper.insert(deviceDataUpload);
        // 返回
        return deviceDataUpload.getId();
    }

    @Override
    public void updateDeviceDataUpload(DeviceDataUploadUpdateReqVO updateReqVO) {
        // 校验存在
        validateDeviceDataUploadExists(updateReqVO.getId());
        // 更新
        DeviceDataUploadDO updateObj = DeviceDataUploadConvert.INSTANCE.convert(updateReqVO);
        deviceDataUploadMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceDataUpload(Integer id) {
        // 校验存在
        validateDeviceDataUploadExists(id);
        // 删除
        deviceDataUploadMapper.deleteById(id);
    }

    private void validateDeviceDataUploadExists(Integer id) {
        if (deviceDataUploadMapper.selectById(id) == null) {
            throw exception(DEVICE_DATA_UPLOAD_NOT_EXISTS);
        }
    }

    @Override
    public DeviceDataUploadDO getDeviceDataUpload(Integer id) {
        return deviceDataUploadMapper.selectById(id);
    }

    @Override
    public List<DeviceDataUploadDO> getDeviceDataUploadList(Collection<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return deviceDataUploadMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DeviceDataUploadDO> getDeviceDataUploadPage(DeviceDataUploadPageReqVO pageReqVO) {
        return deviceDataUploadMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DeviceDataUploadDO> getDeviceDataUploadList(DeviceDataUploadExportReqVO exportReqVO) {
        return deviceDataUploadMapper.selectList(exportReqVO);
    }

}
